//
//  KDKeyWordEditController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/8.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDKeyWordEditController.h"
#import "KDMainwordAddView.h"
#import "KDMainWordAddViewModel.h"
#import "KDNoticeView.h"
#import "KDEditeCellModel.h"
#define CELL_HEIGHT 40
#define NOTICE_LINE_HEIGHT 21

@interface KDKeyWordEditController ()<KDMainwordAddViewDelegate>
/**
 *  主要的View
 */
@property (nonatomic, weak) KDMainwordAddView *mainView;
/**
 *  注意的View
 */
@property (nonatomic, weak) KDNoticeView *noticeView;
/**
 *  将要传到父界面的关键词组
 */
@property (nonatomic, copy) NSString * wordName;

@end

@implementation KDKeyWordEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
    [self setUpNoticeView];
    [self setUpMainView];
    [self setUpNavBar];
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    [self.view addGestureRecognizer:hide];
}

- (void)setUpNoticeView{
    KDNoticeView *noticeView = [KDNoticeView noticeView];
    noticeView.frame = CGRectMake(0, CELL_HEIGHT +10, screenW, NOTICE_LINE_HEIGHT + 15);
    noticeView.title = (self.type - 1)?@"请输入不匹配词": @"请输入微博关键词";
    self.noticeView = noticeView;
    [self.view addSubview:noticeView];
}

- (void)setUpMainView{
    KDMainwordAddView *mainView = [KDMainwordAddView wordView];
    self.mainView = mainView;
    KDMainWordAddViewModel *model = [[KDMainWordAddViewModel alloc]init];

    model.infoArr = self.infoArr;
    
    model.wordTfPlaceHolder = @"添加关键词组";
    model.single = NO;
    model.type = self.type;
    mainView.model = model;
    self.mainView.frame = CGRectMake(0, 10, screenW, CELL_HEIGHT * (mainView.model.infoArr.count +1));
    mainView.delegate = self;

//    mainView.wordTfPlaceHolder = (self.type)?@"请输入不匹配词": @"请输入微博关键词";
    
//    mainView.single = NO;
    //    KDLog(@"%@", NSStringFromCGRect(mainView.frame));
    [self.view addSubview:mainView];
}
- (void)setUpNavBar{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
}

/**
 *  返回按钮被点击了
 */
- (void)backViewClick{
    if (_delegate && [_delegate respondsToSelector:@selector(editController:finishedEditWith:)]) {
        [_delegate editController:self finishedEditWith:self.wordName];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - KDMainwordAddViewDelegate
- (void)KDMainwordAddView:(KDMainwordAddView *)mainWordView HeightChangeWithIfAdd:(BOOL)add AndHeight:(CGFloat)height{
    NSMutableString *str = [NSMutableString string] ;
    if (self.mainView.model.infoArr.count) {
        
        for (KDEditeCellModel *model in self.mainView.model.infoArr) {
            NSString *word = [NSString stringWithFormat:@"%@,",model.name];
            [str appendFormat:@"%@", word];
        }
        [str deleteCharactersInRange:NSMakeRange(str.length -1, 1)];
    }
    self.wordName = str;
}
#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self.view endEditing:YES];
}
@end
